<?php
/**
 * Zend_Service_Netflix_Title Class.
 *
 * @author Armando Padilla, armando_padilla_81@yahoo.com
 * @package Zend_Service_Netflix
 * 
 */ 
require_once "Utils.php";

class Zend_Service_Netflix_Title { 
 
	/**
	 * Title Id
	 *
	 * @var String
	 */ 
	public $id; 
	
	/**
	 * Title Id as URL
	 * 
	 */
	public $titleIDAsURL;
	
	/**
	 * Season Id. (TV)
	 *
	 * @var String
	 */
	public $season_id = "";
	
	
	/**
	 * Series Id (TV)
	 *
	 * @var String
	 */
	public $series_id = "";
	 
	
	/**
	 * Full Title
	 *
	 * @var String
	 */ 
	public $title = ""; 
	 
	/**
	 * Short Title.
	 *
	 * @var String
	 */ 
	public $titleShort = ""; 
	 
	/**
	 * API URL Ref.
	 *
	 * @var String
	 */ 
	public $synops = ""; 
	
	 
	/**
	 * URL to location of small box art.
	 *
	 * @var String
	 */ 
	public $boxArtSmall = ""; 
	 
	/**
	 * URL to location of medium box art.
	 *
	 * @var String
	 */ 
	public $boxArtMedium = ""; 
	 
	/**
	 * URL to location of large box art.
	 *
	 * @var String
	 */ 
	public $boxArtLarge = ""; 
	 
	/**
	 * Release Year.
	 *
	 * @var String
	 */ 
	public $releaseYear = ""; 
	 
	/**
	 * MPAA Rating.
	 *
	 * @var String
	 */ 
	public $mpaaRating = ""; 
	 
	/**
	 * TV Rating.
	 *
	 * @var String
	 */
	public $tvRating   = "";
	
	/**
	 * Genre.
	 *
	 * @var Array
	 */ 
	public $genres = array(); 
	 
	/**
	 * Average Rating.
	 *
	 * @var String
	 */ 
	public $avarageRating; 
	 
	 
	/**
	 * Cast list.
	 *
	 * @var Array
	 */ 
	public $cast = array(); 
	 
	/**
	 * Director list.
	 *
	 * @var Array
	 */ 
	public $directors = array(); 
	 
	 
	/**
	 * Awards won.
	 *
	 * @var Array
	 */ 
	public $awardsWon = array(); 
	
	/**
	 * Awards the title was nominated form.
	 *
	 * @var Array
	 */
	public $awardsNominated = array();
	 
	/**
	 * Bonus material list.
	 *
	 * @var Array
	 */ 
	public $bonusMaterial = array(); 
	 
	 
	/**
	 * Screen Format List.
	 *
	 * @var Array
	 */ 
	public $screenFormats = array(); 
	 
	/**
	 * Format List.
	 *
	 * @var Array
	 */ 
	public $formatAvailability = array(); 
	 
	/**
	 * Language & Audio Format List.
	 *
	 * @var Array
	 */ 
	public $languageAndSound = array(); 
	 
	 
	/**
	 * URL of movie on Netflix.
	 *
	 * @var unknown_type
	 */ 
	public $netflixUrl;  
	 
	 
	/**
	 * Official Web Site URL.
	 *
	 * @var unknown_type
	 */ 
	public $officialTitleUrl; 
	 
	/**
	 * List of similar titles.
	 *
	 * @var Array
	 */ 
	public $similarTitles = array(); 
	 
	
	/**
	 * Runtime. In minutes.
	 *
	 * @var int
	 */
	public $runtime;
	 
	 
	/**
	 * Constructor
	 */ 
	public function __construct(){} 
	
         
	/**
	 * Load Data from XML.
	 *
	 * @param SimpleXMLElement $simpleXmlObject
	 */ 
	public function loadXML(SimpleXMLElement $simpleXmlObject){
		
		$this->title         = (string)$simpleXmlObject->title['regular'];
		$this->titleShort    = (string)$simpleXmlObject->title['short'];
		$this->boxArtSmall   = (string)$simpleXmlObject->box_art['small'];
		$this->boxArtMedium  = (string)$simpleXmlObject->box_art['medium'];
		$this->boxArtLarge   = (string)$simpleXmlObject->box_art['large'];
		$this->releaseYear   = (string)$simpleXmlObject->release_year;
		$this->avarageRating = (string)$simpleXmlObject->average_rating;
		$this->synops        = (string)$simpleXmlObject->link->synopsis;
		$this->runtime 		 = (string)$simpleXmlObject->runtime;
		
		//Check if this is a series or a movie title
		$this->titleIDAsURL = (string)$simpleXmlObject->id;
		
		if(strpos((string)$simpleXmlObject->id, "series")){
			
			$this->season_id = Zend_Service_Netflix_Utils::parseSeasonId((string)$simpleXmlObject->id);
			$this->series_id = Zend_Service_Netflix_Utils::parseSeriesId((string)$simpleXmlObject->id);
			
		}else{
			
			$this->id  = Zend_Service_Netflix_Utils::parseTitleId((string)$simpleXmlObject->id);
		
		}
		
		//Load data for XPath use.
		$simpleXmlObject->asXML();
		$xml  = simplexml_load_string($simpleXmlObject->asXML());
			
		//Get the Netflix Profile Page
		$netflixUrl 		= $xml->xpath("link[@title='web page']");
		if(isset($netflixUrl[0]['href'])){
			$this->netflixUrl 	= (string)$netflixUrl[0]['href'];
		}
		
		//Get the official web site.
		$officialTitleURL 		= $xml->xpath("link[@title='official webpage']");
		if(isset($officialTitleURL[0]['href'])){
			$this->officialTitleUrl = (string)$officialTitleURL[0]['href'];
		}
		

		//Get all Cast.
		$cast = $xml->xpath("link[@title='cast']/people/link");
		if(is_array($cast)){
			foreach($cast as $person){
				
				$id   = Zend_Service_Netflix_Utils::parsePersonId($person['href']);
				$name = (string)$person['title'];
				
				array_push($this->cast, array("id"   => $id, 
											  "name" => $name));
			}
		}
		
		
		//Get all Directors
		$directors = $xml->xpath("link[@title='directors']/people/link");
		if(is_array($directors)){
			foreach($directors as $person){
				
				$id   = Zend_Service_Netflix_Utils::parsePersonId($person['href']);
				$name = (string)$person['title'];
				
				array_push($this->directors, array("id"   => $id,
												   "name" => $name));
				
			}
		}
		
		//Get all Awards
		$awards = $xml->xpath("link[@title='awards']/awards");
		if(is_array($awards)){
			foreach($awards as $award){
				
				//Get the Awards Won
				foreach($award->award_winner as $winner){
				
					$year     = (string)$winner['year'];
					$awardWon = (string)$winner->category['term'];
					
					array_push($this->awardsWon, array("year"      => $year,
												       "award_won" => $awardWon)); 
					
				}
				
				//Get Nominations
				foreach($award->award_nominee as $nominee){
					
					$year      = (string)$nominee['year'];
					$nominated = (string)$nominee->category['term'];
					
					array_push($this->awardsNominated, array("year"          => $year,
												             "award_nominee" =>$nominated)); 
				}
							
			}
		}
		
		//Get Title Formats
		$formats = $xml->xpath("link[@title='formats']/delivery_formats/availability/category");
		if(is_array($formats)){
			foreach($formats as $format){
				
				$type = (string)$format['term'];
				
				array_push($this->formatAvailability, $type);	
				
			}
		}
		
		//Get the Screen Formats
		$screenFormats = $xml->xpath("link[@title='screen formats']/screen_formats/screen_format/category");
		if(is_array($screenFormats)){
			foreach($screenFormats as $format){
				
				$type = (string)$format['term'];
				
				array_push($this->screenFormats, $type);
				
			}
		}
		
		//Get all the Audio formats.
		$audioFormats = $xml->xpath("link[@title='languages and audio']/languages_and_audio/language_audio_format/category");
		if(is_array($audioFormats)){
			foreach($audioFormats as $audioFormat){
				
				array_push($this->languageAndSound, (string)$audioFormat['term']);
				
			}
		}
		
		
		//Get all Similar Titles.
		$similarTitles = $xml->xpath("link[@title='similars']/catalog_titles/link");
		if(is_array($similarTitles)){
			foreach($similarTitles as $title){
				
				$id 	= Zend_Service_Netflix_Utils::parseTitleId($title['href']);
				$title 	= (string)$title['title'];
				
				array_push($this->similarTitles, array("id" 	=> $id, 
													   "title" 	=> $title));
				
			}
		}
		
		//Get the bonus material.
		$bonusMaterial = $xml->xpath("link[@title='bonus materials']/bonus_materials/link");
		if(is_array($bonusMaterial)){
			foreach($bonusMaterial as $material){
				
				$id 	= Zend_Service_Netflix_Utils::parseTitleId((string)$material['href']);
				$title 	= (string)$material['title'];
				array_push($this->bonusMaterial, array("id"    => $id,
													   "title" => $title));
				
			}
		}	
		
		foreach($simpleXmlObject->category as $elem){
			
			$value  = (string)$elem['label'];
			$scheme = (string)$elem['scheme'];
			
			//mpaa rating
			if(strpos($scheme, 'mpaa_ratings')){
				$this->mpaaRating = $value;
			}
			
			//tv rating
			if(strpos($scheme, 'tv_ratings')){
				$this->tvRating = $value;
			}
			
			//genres
			if(strpos($scheme, 'genres')){
				array_push($this->genres, $value);
			}
			
			
				
		}
		
	}
	

}